import 'package:flutter/material.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/CustomCalendar.dart';
import 'package:flutter_app/widget/CustomDialog.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';

import 'duty_dialog_logic.dart';

/**
 * @Description: 窗口值班 弹窗  自适应高度
 * @Author: 下雨了
 * @CreateDate: 2021-10-29 10:36
 * @UpdateUser: 下雨了
 * @UpdateDate:  2021-10-29 10:36
 * @UpdateRemark: 自适应高度
 *                特此记录 使用 showModalBottomSheet
 *                         设置   isScrollControlled: true,
 *                在根节点中用SingleChildScrollView来包裹Column即可让弹窗高度适应布局高度
 * @Version: 1.1
 */

/// 自定义回调 刷新数据
typedef OnRefreshCallback = void Function(String collectId);

class DutyDialogPage extends StatefulWidget {
  final String? title;

  final String collectId;

  final String status; // 报名状态 0未报名 1已报名

  final OnRefreshCallback onRefreshCallback;

  const DutyDialogPage({
    Key? key,
    this.title,
    required this.collectId,
    required this.status,
    required this.onRefreshCallback,
  }) : super(key: key);

  @override
  _DutyDialogPageState createState() => _DutyDialogPageState();
}

class _DutyDialogPageState extends State<DutyDialogPage> {
  final logic = Get.put(DutyDialogLogic());
  final state = Get.find<DutyDialogLogic>().state;

  @override
  void initState() {
    super.initState();
    state.collectId = widget.collectId;
    state.status = widget.status;
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DutyDialogLogic>(builder: (logic) {
      return SingleChildScrollView(
        child: Column(
          children: [
            Align(alignment: Alignment.centerRight,
              child: Container(
                margin: EdgeInsets.only(top: 15.5, right: 16.5),
                child: GestureDetector(
                  child: Icon(Icons.close_rounded, color: MyColors.FF999999, size: 25,),
                  onTap: (){
                    Navigator.pop(context);
                  },
                ),
              ),
            ),
            Text("选择" + state.currentMonth + "月份值班日期", style: TextStyle(color: MyColors.FF333333, fontSize: 18.sp),),
            Container(
              margin: EdgeInsets.only(left: 10.r,right: 10.r),
              child: CustomCalendar(
                key: logic.customCalendarKey,
                focusedDay: state.focusedDay,
                firstDay: state.firstDay,
                lastDay: state.lastDay,
                disabledDays: state.disabledDays,
                selectedDays: state.selectedDays,
                holidayDays: state.holidayDays,
                onDateTimeCallback: (List<String> checkedTime) {
                  state.checkedTime = checkedTime;
                },
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 14.r),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Row(
                    children: [
                      Container(
                        width: 12.5.r,
                        height: 12.5.r,
                        color: MyColors.FFEBEBEB,
                      ),
                      Container(margin: EdgeInsets.only(left: 5.r)),
                      Text("不可报名", style: TextStyle(color: MyColors.FF777777, fontSize: 13.sp),),
                    ],
                  ),
                  Row(
                    children: [
                      Container(
                        width: 12.5.r,
                        height: 12.5.r,
                        color: MyColors.FFDFEFFC,
                      ),
                      Container(margin: EdgeInsets.only(left: 5.r)),
                      Text("已报名0人", style: TextStyle(color: MyColors.FF777777, fontSize: 13.sp),)
                    ],
                  ),
                  Row(
                    children: [
                      Container(
                        width: 12.5.r,
                        height: 12.5.r,
                        color: MyColors.FFA7D4F8,
                      ),
                      Container(margin: EdgeInsets.only(left: 5.r)),
                      Text("已报名1人", style: TextStyle(color: MyColors.FF777777, fontSize: 13.sp),)
                    ],
                  ),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 16.r,right: 16.r, top: 8.r),
              child: Text("温馨提示：每天最多两名监督员报名，由效能办工作人员审核保留一名监督员值班。",
                style: TextStyle(color: MyColors.FF777777, fontSize: 11.sp),
              ),
            ),
            SafeArea(
              top: false,
              child: Container(
                margin: EdgeInsets.only(top: 21.r, left: 16.r, right: 16.r, bottom: 21.r),
                child: TextButton(
                  child: Text("确认", style: TextStyle(color: MyColors.FFFFFFFF, fontSize: 15.sp, fontWeight: FontWeight.w600),),
                  style: ButtonStyle(
                      minimumSize: MaterialStateProperty.all(Size(MediaQuery.of(context).size.width, 40.r)),
                      backgroundColor: MaterialStateProperty.all<Color>(MyColors.FF2763B9)
                  ),
                  onPressed: () {
                    //  status  报名状态 0未报名 1已报名
                    if (state.status == "0") {
                      if (state.checkedTime.length ==  0 ) {
                        toast.show("请选择值班日期");
                        return;
                      }
                    }
                    logic.postGoScheduleSign(state.collectId, state.checkedTime).then((value){
                      Get.back();
                      if (state.checkedTime.length == 0) {
                        widget.onRefreshCallback.call(state.collectId);
                        toast.show("已取消所有报名");
                        return;
                      }
                      //  status  报名状态 0未报名 1已报名
                      if (state.status == "1") {
                        toast.show("修改成功");
                        return;
                      }
                      if (value) {
                        showDialog(context: context, barrierDismissible: false, builder: (context){
                          return CustomDialog(
                            content: "请按时参加效能投诉窗口值班，感谢您的支持。",
                            onPressed: () {
                              widget.onRefreshCallback.call(widget.collectId);
                              Navigator.of(context).pop("ok");
                            },
                          );
                        });
                      }
                    });
                  },
                ),
              ),
            )
          ],
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<DutyDialogLogic>();
    super.dispose();
  }
}